﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Services.Localization;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:PagingControl runat=server></{0}:PagingControl>")]
    public class AdminPagingControl : WebControl
    {
        private string _CSSClassLinkActive;
        private string _CSSClassLinkInactive;
        private string _CSSClassPagingStatus;
        private int _CurrentPage;
        [AccessedThroughProperty("PageNumbers")]
        private Repeater _PageNumbers;
        private int _PageSize;
        private string _QuerystringParams;
        private string _SkinSrc;
        private int _TabID;
        private int _TotalRecords;
        protected TableCell cellDisplayLinks;
        protected TableCell cellDisplayStatus;
        protected Table tablePageNumbers;
        private int TotalPages = -1;

        private void BindPageNumbers(int TotalRecords, int RecordsPerPage)
        {
            int num = 10;
            if ((((double) TotalRecords) / ((double) RecordsPerPage)) >= 1.0)
            {
                this.TotalPages = Convert.ToInt32(Math.Ceiling((double) (((double) TotalRecords) / ((double) RecordsPerPage))));
            }
            else
            {
                this.TotalPages = 0;
            }
            if (this.TotalPages > 0)
            {
                DataTable table = new DataTable();
                table.Columns.Add("PageNum");
                int num4 = 1;
                int totalPages = this.TotalPages;
                double d = this.CurrentPage - (((double) num) / 2.0);
                if (d < 1.0)
                {
                    d = 1.0;
                }
                if (this.CurrentPage > (((double) num) / 2.0))
                {
                    num4 = (int) Math.Round(Math.Floor(d));
                }
                if (this.TotalPages <= num)
                {
                    totalPages = this.TotalPages;
                }
                else
                {
                    totalPages = (num4 + num) - 1;
                }
                if (totalPages > this.TotalPages)
                {
                    totalPages = this.TotalPages;
                    if ((totalPages - num4) < num)
                    {
                        num4 = (totalPages - num) + 1;
                    }
                }
                if (totalPages > this.TotalPages)
                {
                    totalPages = this.TotalPages;
                }
                if (num4 < 1)
                {
                    num4 = 1;
                }
                int num6 = totalPages;
                for (int i = num4; i <= num6; i++)
                {
                    DataRow row = table.NewRow();
                    row["PageNum"] = i;
                    table.Rows.Add(row);
                }
                this.PageNumbers.DataSource = table;
                this.PageNumbers.DataBind();
            }
        }

        protected override void CreateChildControls()
        {
            this.tablePageNumbers = new Table();
            this.cellDisplayStatus = new TableCell();
            this.cellDisplayLinks = new TableCell();
            this.cellDisplayStatus.CssClass = "Normal";
            this.cellDisplayLinks.CssClass = "Normal";
            if (this.CssClass == "")
            {
                this.tablePageNumbers.CssClass = "PagingTable";
            }
            else
            {
                this.tablePageNumbers.CssClass = this.CssClass;
            }
            int num = this.tablePageNumbers.Rows.Add(new TableRow());
            this.PageNumbers = new Repeater();
            PageNumberLinkTemplate template = new PageNumberLinkTemplate(this);
            this.PageNumbers.ItemTemplate = template;
            this.BindPageNumbers(this.TotalRecords, this.PageSize);
            this.cellDisplayStatus.HorizontalAlign = HorizontalAlign.Left;
            Unit unit2 = new Unit("50%");
            this.cellDisplayStatus.Width = unit2;
            this.cellDisplayLinks.HorizontalAlign = HorizontalAlign.Right;
            unit2 = new Unit("50%");
            this.cellDisplayLinks.Width = unit2;
            int totalPages = this.TotalPages;
            if (totalPages == 0)
            {
                totalPages = 1;
            }
            LiteralControl child = new LiteralControl(string.Format(DotNetNuke.Services.Localization.Localization.GetString("Pages"), this.CurrentPage.ToString(), totalPages.ToString()));
            this.cellDisplayStatus.Controls.Add(child);
            this.tablePageNumbers.Rows[num].Cells.Add(this.cellDisplayStatus);
            this.tablePageNumbers.Rows[num].Cells.Add(this.cellDisplayLinks);
        }

        private string CreateURL(string CurrentPage)
        {
            string str2 = "";
            if (this._SkinSrc != "")
            {
                str2 = "?SkinSrc=" + this._SkinSrc;
            }
            if (this.QuerystringParams != "")
            {
                if (CurrentPage != "")
                {
                    return (Globals.NavigateURL(this.TabID, "", new string[] { this.QuerystringParams, "currentpage=" + CurrentPage }) + str2);
                }
                return (Globals.NavigateURL(this.TabID, "", new string[] { this.QuerystringParams }) + str2);
            }
            if (CurrentPage != "")
            {
                return (Globals.NavigateURL(this.TabID, "", new string[] { "currentpage=" + CurrentPage }) + str2);
            }
            return (Globals.NavigateURL(this.TabID) + str2);
        }

        private string GetFirstLink()
        {
            if ((this.CurrentPage > 1) && (this.TotalPages > 0))
            {
                return ("<a href=\"" + this.CreateURL("1") + "\" class=\"" + this.CSSClassLinkActive + "\">" + DotNetNuke.Services.Localization.Localization.GetString("First", DotNetNuke.Services.Localization.Localization.SharedResourceFile) + "</a>");
            }
            return ("<span class=\"" + this.CSSClassLinkInactive + "\">" + DotNetNuke.Services.Localization.Localization.GetString("First", DotNetNuke.Services.Localization.Localization.SharedResourceFile) + "</span>");
        }

        private string GetLastLink()
        {
            if ((this.CurrentPage != this.TotalPages) & (this.TotalPages > 0))
            {
                return ("<a href=\"" + this.CreateURL(this.TotalPages.ToString()) + "\" class=\"" + this.CSSClassLinkActive + "\">" + DotNetNuke.Services.Localization.Localization.GetString("Last", DotNetNuke.Services.Localization.Localization.SharedResourceFile) + "</a>");
            }
            return ("<span class=\"" + this.CSSClassLinkInactive + "\">" + DotNetNuke.Services.Localization.Localization.GetString("Last", DotNetNuke.Services.Localization.Localization.SharedResourceFile) + "</span>");
        }

        private string GetLink(int PageNum)
        {
            if (PageNum == this.CurrentPage)
            {
                return ("<span class=\"" + this.CSSClassLinkInactive + "\">[" + PageNum.ToString() + "]</span>");
            }
            return ("<a href=\"" + this.CreateURL(PageNum.ToString()) + "\" class=\"" + this.CSSClassLinkActive + "\">" + PageNum.ToString() + "</a>");
        }

        private string GetNextLink()
        {
            if ((this.CurrentPage != this.TotalPages) & (this.TotalPages > 0))
            {
                string[] strArray = new string[] { "<a href=\"", this.CreateURL((this.CurrentPage + 1).ToString()), "\" class=\"", this.CSSClassLinkActive, "\">", DotNetNuke.Services.Localization.Localization.GetString("Next", DotNetNuke.Services.Localization.Localization.SharedResourceFile), "</a>" };
                return string.Concat(strArray);
            }
            return ("<span class=\"" + this.CSSClassLinkInactive + "\">" + DotNetNuke.Services.Localization.Localization.GetString("Next", DotNetNuke.Services.Localization.Localization.SharedResourceFile) + "</span>");
        }

        private string GetPreviousLink()
        {
            if ((this.CurrentPage > 1) && (this.TotalPages > 0))
            {
                string[] strArray = new string[] { "<a href=\"", this.CreateURL((this.CurrentPage - 1).ToString()), "\" class=\"", this.CSSClassLinkActive, "\">", DotNetNuke.Services.Localization.Localization.GetString("Previous", DotNetNuke.Services.Localization.Localization.SharedResourceFile), "</a>" };
                return string.Concat(strArray);
            }
            return ("<span class=\"" + this.CSSClassLinkInactive + "\">" + DotNetNuke.Services.Localization.Localization.GetString("Previous", DotNetNuke.Services.Localization.Localization.SharedResourceFile) + "</span>");
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (this.PageNumbers == null)
            {
                this.CreateChildControls();
            }
            StringBuilder builder2 = new StringBuilder();
            builder2.Append(this.GetFirstLink() + "&nbsp;&nbsp;&nbsp;");
            builder2.Append(this.GetPreviousLink() + "&nbsp;&nbsp;&nbsp;");
            StringBuilder sb = new StringBuilder(0x400);
            this.PageNumbers.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
            builder2.Append(sb.ToString());
            builder2.Append(this.GetNextLink() + "&nbsp;&nbsp;&nbsp;");
            builder2.Append(this.GetLastLink() + "&nbsp;&nbsp;&nbsp;");
            this.cellDisplayLinks.Controls.Add(new LiteralControl(builder2.ToString()));
            this.tablePageNumbers.RenderControl(output);
        }

        [Category("Behavior"), DefaultValue("Normal"), Bindable(true)]
        public string CSSClassLinkActive
        {
            get
            {
                if (this._CSSClassLinkActive == "")
                {
                    return "CommandButton";
                }
                return this._CSSClassLinkActive;
            }
            set
            {
                this._CSSClassLinkActive = value;
            }
        }

        [Category("Behavior"), Bindable(true), DefaultValue("CommandButton")]
        public string CSSClassLinkInactive
        {
            get
            {
                if (this._CSSClassLinkInactive == "")
                {
                    return "NormalDisabled";
                }
                return this._CSSClassLinkInactive;
            }
            set
            {
                this._CSSClassLinkInactive = value;
            }
        }

        [DefaultValue("Normal"), Bindable(true), Category("Behavior")]
        public string CSSClassPagingStatus
        {
            get
            {
                if (this._CSSClassPagingStatus == "")
                {
                    return "Normal";
                }
                return this._CSSClassPagingStatus;
            }
            set
            {
                this._CSSClassPagingStatus = value;
            }
        }

        [Bindable(true), Category("Behavior"), DefaultValue("1")]
        public int CurrentPage
        {
            get
            {
                return this._CurrentPage;
            }
            set
            {
                this._CurrentPage = value;
            }
        }

        protected virtual Repeater PageNumbers
        {
            get
            {
                return this._PageNumbers;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._PageNumbers = value;
            }
        }

        [Bindable(true), DefaultValue("10"), Category("Behavior")]
        public int PageSize
        {
            get
            {
                return this._PageSize;
            }
            set
            {
                this._PageSize = value;
            }
        }

        [DefaultValue(""), Category("Behavior"), Bindable(true)]
        public string QuerystringParams
        {
            get
            {
                return this._QuerystringParams;
            }
            set
            {
                this._QuerystringParams = value;
            }
        }

        [Bindable(true), Category("Behavior"), DefaultValue("")]
        public string SkinSrc
        {
            get
            {
                return this._SkinSrc;
            }
            set
            {
                this._SkinSrc = value;
            }
        }

        [DefaultValue("-1"), Bindable(true), Category("Behavior")]
        public int TabID
        {
            get
            {
                return this._TabID;
            }
            set
            {
                this._TabID = value;
            }
        }

        [Category("Behavior"), Bindable(true), DefaultValue("0")]
        public int TotalRecords
        {
            get
            {
                return this._TotalRecords;
            }
            set
            {
                this._TotalRecords = value;
            }
        }

        public class PageNumberLinkTemplate : ITemplate
        {
            private AdminPagingControl _PagingControl;
            private static int itemcount = 0;

            public PageNumberLinkTemplate(AdminPagingControl ctlPagingControl)
            {
                this._PagingControl = ctlPagingControl;
            }

            private void BindData(object sender, EventArgs e)
            {
                Literal literal = (Literal) sender;
                RepeaterItem namingContainer = (RepeaterItem) literal.NamingContainer;
                literal.Text = this._PagingControl.GetLink(Convert.ToInt32(RuntimeHelpers.GetObjectValue(DataBinder.Eval(RuntimeHelpers.GetObjectValue(namingContainer.DataItem), "PageNum")))) + "&nbsp;&nbsp;";
            }

            public void InstantiateIn(Control container)
            {
                Literal child = new Literal();
                child.DataBinding += new EventHandler(this.BindData);
                container.Controls.Add(child);
            }
        }
    }
}

